package com.ifcar.app.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.ifcar.app.activity.MyApplication;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.rss.GetContent;
import com.ifcar.app.util.ModelUtils;
import com.ifcar.app.util.StringUtil;

public class Vote {
	public static List<Map<String, Object>> getVoteList(Context context) {
		return GetContent.getVoteList(StringUtil.voteList_url, context);
	}

	public static boolean doVote(Context context, SQLiteDatabase db,
			String voteId, String itemId) {
		String res = GetContent.doVote(voteId, itemId, context);

		if (TextUtils.isEmpty(res)) {
			return false;
		}
		try {
			JSONObject obj = new JSONObject(res);
			String r = obj.getString("result");
			if ("success".equals(r)) {
				// 保存数据库
				ContentValues values = new ContentValues();
				values.put(NewsSQLiteOpenHelper.VOTE_ID, voteId);
				db.insert(NewsSQLiteOpenHelper.VOTE_TABLE, null, values);

				return true;

			}
		} catch (JSONException e) {
			e.printStackTrace();

		}
		return false;
	}

	/**
	 * 把所有投票数据转成串
	 * 
	 * @param data
	 * @return
	 */
	public static ArrayList<String> data2StringArray(
			List<Map<String, Object>> data) {
		ArrayList<String> voteArray = new ArrayList<String>();
		if (data != null && data.size() > 0) {
			for (Map<String, Object> item : data) {
				JSONObject json = ModelUtils.map2Json(item);
				if (json == null) {
					voteArray.add("");
				} else {
					voteArray.add(json.toString());
				}
			}
		}
		return voteArray;
	}

	/**
	 * 把串转换成投票数据
	 * 
	 * @param data
	 * @return
	 */
	public static List<Map<String, Object>> stringArray2Data(
			ArrayList<String> voteArray) {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		if (voteArray != null && voteArray.size() > 0) {
			for (String item : voteArray) {
				Map<String, Object> map = null;

				try {
					if (!TextUtils.isEmpty(item)) {
						JSONObject json = new JSONObject(item);
						map = ModelUtils.json2Map(json);
					}
				} catch (Exception e) {
				}

				if (map == null) {
					map = new HashMap<String, Object>();
				}
				data.add(map);
			}
		}
		return data;
	}

	/**
	 * 获取单条投票数量
	 * 
	 * @param itemData
	 * @return
	 */
	public static int getVoteItemNum(Map<String, Object> itemData) {
		int num = 0;

		try {
			num = Integer.valueOf((String) ModelUtils.getValue(itemData,
					StringUtil.VOTE_ITEM_NUM));
		} catch (Exception e) {
		}

		return num;
	}

	/**
	 * 获取投票总数量
	 * 
	 * @param data
	 * @return
	 */
	public static int getVoteTotalNum(Map<String, Object> data) {
		return getVoteTotalNum((List<Map<String, Object>>) ModelUtils.getValue(
				data, StringUtil.VOTE_ITEM_LIST));
	}

	/**
	 * 获取投票总数量
	 * 
	 * @param itemList
	 * @return
	 */
	public static int getVoteTotalNum(List<Map<String, Object>> itemList) {
		if (itemList == null) {
			return 0;
		}
		int totalNum = 0;

		for (int i = itemList.size() - 1; i >= 0; i--) {
			totalNum += getVoteItemNum(itemList.get(i));

		}
		return totalNum;
	}

	/**
	 * 是否已经参加过投票
	 * 
	 * @param data
	 * @return
	 */
	public static boolean isVoted(Context context, Map<String, Object> data) {
		String voteId = (String) ModelUtils.getValue(data, StringUtil.VOTE_ID);
		boolean hasSubmited = false;
		if (!TextUtils.isEmpty(voteId)) {
			MyApplication app = (MyApplication) context.getApplicationContext();
			SQLiteDatabase db = app.getSQLiteDatabase();
			Cursor cur = db.query(NewsSQLiteOpenHelper.VOTE_TABLE,
					new String[] { NewsSQLiteOpenHelper.VOTE_ID },
					NewsSQLiteOpenHelper.VOTE_ID + "=" + voteId, null, null,
					null, NewsSQLiteOpenHelper.VOTE_AUTO_ID + " desc");

			if (cur != null) {
				hasSubmited = cur.getCount() > 0;
				cur.close();
			}
		}
		return hasSubmited;
	}

}
